﻿using ProjScan.RecordManager;
using ProjScan.RecordManager.AssetData;
using ProjScan.ScanRule.Attr;
using UnityEditor;

namespace ProjScan.ScanRule.ProjectAssets.Texture2D
{
    [TextureRuleAttr]
    internal class Texture_CompressionFormat : RecordRuleImp<TextureAssetData, TextureRecordManager>
    {
        protected override bool IsOK(TextureAssetData assetData)
        {
            return !PlatformNotCompressed(assetData.assetImporter);
        }

        private bool PlatformNotCompressed(TextureImporter imp)
        {
            string platform = null;
            if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
            {
                platform = "Android";
            }
            else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS)
            {
                platform = "iPhone";
            }
            else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows || EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows64)
            {
                platform = "Standalone";
            }

            TextureImporterFormat textureImporterFormat = TextureImporterFormat.RGBA32;
            TextureImporterPlatformSettings platformTextureSettings = imp.GetPlatformTextureSettings(platform);
            if (platformTextureSettings.overridden)
            {
                textureImporterFormat = platformTextureSettings.format;
                if (textureImporterFormat == TextureImporterFormat.Automatic && platformTextureSettings.textureCompression == TextureImporterCompression.Uncompressed)
                {
                    textureImporterFormat = TextureImporterFormat.RGBA32;
                }
            }
            else
            {
                TextureImporterPlatformSettings defaultPlatformTextureSettings = imp.GetDefaultPlatformTextureSettings();
                textureImporterFormat = defaultPlatformTextureSettings.format;
                if (textureImporterFormat == TextureImporterFormat.Automatic && defaultPlatformTextureSettings.textureCompression == TextureImporterCompression.Uncompressed)
                {
                    textureImporterFormat = TextureImporterFormat.RGBA32;
                }
            }

            return textureImporterFormat == TextureImporterFormat.ARGB32 || textureImporterFormat == TextureImporterFormat.RGBA32 || textureImporterFormat == TextureImporterFormat.RGB24;
        }
    }
}